import json,requests
import time
class XX():
    data = {'contestId': '292','paperId': '220'}
    headers = {
        'Cookie': 'Hm_lvt_65afb4267d7306d3059244fb2f02c4c3=1604627606; Hm_lpvt_65afb4267d7306d3059244fb2f02c4c3=1604627606; Authorization=eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIyMDE5MDIwMTAwNDUiLCJ1c2VySWQiOiJlYmE3NDUzYmZhNDgxMWVhYTljZWQ4OWQ2NzI0MGYxOCIsIm5hbWUiOiLnjovmtbfpuY8iLCJleHAiOjE2MDQ3MTQwMjl9.UP3AfMYBOySAptDp7T3Brax6Lg4OaA3uaH0L_mZOhtA8oBOiu9X7Xaj0Tb5eIbCTFPhk-BkTHoBFQYLReUMQHef7gnVL7VUGRfVefP7L_UE-LNInHuOLaKnIrsagJfN7QTef4r01fs7rPaYZeL_eNLwFHxL7Dy5ZqGMV2vUfQWw'
        ,
        'Authorization': 'eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIyMDE5MDIwMTAwNDUiLCJ1c2VySWQiOiJlYmE3NDUzYmZhNDgxMWVhYTljZWQ4OWQ2NzI0MGYxOCIsIm5hbWUiOiLnjovmtbfpuY8iLCJleHAiOjE2MDQ3MTQwMjl9.UP3AfMYBOySAptDp7T3Brax6Lg4OaA3uaH0L_mZOhtA8oBOiu9X7Xaj0Tb5eIbCTFPhk-BkTHoBFQYLReUMQHef7gnVL7VUGRfVefP7L_UE-LNInHuOLaKnIrsagJfN7QTef4r01fs7rPaYZeL_eNLwFHxL7Dy5ZqGMV2vUfQWw'
        ,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
        ,
        'Host': 'xxsy.apabi.cn',
        'Origin': 'http://xxsy.apabi.cn',
        'Referer': 'http://xxsy.apabi.cn/study/',
        'Content-Type': 'application/json;charset=UTF-8'
    }
    def ID(self):
        url = 'http://xxsy.apabi.cn/courseexam/font/exam/mock/startExam'
        r =requests.post(url,headers=self.headers,data=json.dumps(self.data))
        jd = json.loads(r.text)
        return jd['data']
    def Tjiao(self):
        url ='http://xxsy.apabi.cn/courseexam/font/exam/mock/endAnswer'
        data = {
                'answers': [],
                'examRecordId': self.ID()
            }
        requests.post(url,headers=self.headers,data=json.dumps(data))

    def Qing(self):
        self.Tjiao()
        self.Xuan()
        #self.Duo()
        #self.Pan()
    def Xuan(self):
        url ='http://xxsy.apabi.cn/courseexam/font/exam/mock/getQuestionAnalysis'
        data = {
            'contestId': '292',
            'endBit': '30',
            'questionType': '0',
            'startBit': '0',
            'userId': 'eba7453bfa4811eaa9ced89d67240f18'
        }
        r =requests.post(url,headers=self.headers,data=json.dumps(data))
        r.encoding='utf-8'
        jd = json.loads(r.text)
        for i in jd.get('data'):
            try:
                #test = str.format('{},{},{}\n',i.get('title').replace(',','，'),str.format('{}|{}|{}|{}',i.get('ops')[0]['name'],i.get('ops')[1]['name'],i.get('ops')[2]['name'],i.get('ops')[3]['name']).replace(',','，'),i.get('realAnswer'))
                test = str.format('{},{},{}\n', i.get('title').replace(',', '，'),
                                  str.format('{}.{}|{}.{}|{}.{}|{}.{}',i.get('ops')[0]['selectId'], i.get('ops')[0]['name'],i.get('ops')[1]['selectId'], i.get('ops')[1]['name'],i.get('ops')[2]['selectId'],
                                             i.get('ops')[2]['name'],i.get('ops')[3]['selectId'], i.get('ops')[3]['name']).replace(',', '，'),
                                  i.get('realAnswer'))
                test.encode('utf-8')
                self.Download('国模拟选择题.csv',test)
            except:
                print()

    def Duo(self):
        url ='http://xxsy.apabi.cn/courseexam/font/exam/mock/getQuestionAnalysis'
        data = {
            'contestId': '292',
            'endBit': '15',
            'questionType': '1',
            'startBit': '0',
            'userId': 'eba7453bfa4811eaa9ced89d67240f18'
        }
        r =requests.post(url,headers=self.headers,data=json.dumps(data))
        jd = json.loads(r.text)
        for i in jd.get('data'):
            try:
                test = str.format('{},{},{}\n', i.get('title').replace(',', '，'),
                                  str.format('{}.{}|{}.{}|{}.{}|{}.{}',i.get('ops')[0]['selectId'], i.get('ops')[0]['name'],i.get('ops')[1]['selectId'], i.get('ops')[1]['name'],i.get('ops')[2]['selectId'],
                                             i.get('ops')[2]['name'],i.get('ops')[3]['selectId'], i.get('ops')[3]['name']).replace(',', '，'),
                                  i.get('realAnswer'))
                test.encode('utf-8')
                self.Download('国模拟多选题.csv',test)
            except:
                print()
    def Pan(self):
        url ='http://xxsy.apabi.cn/courseexam/font/exam/mock/getQuestionAnalysis'
        data = {
            'contestId': '292',
            'endBit': '15',
            'questionType': '3',
            'startBit': '0',
            'userId': 'eba7453bfa4811eaa9ced89d67240f18'
        }
        r =requests.post(url,headers=self.headers,data=json.dumps(data))
        jd = json.loads(r.text)
        for i in jd.get('data'):
            try:
                pan = '错'
                if i.get('realAnswer') == 1:
                    pan = '对'
                test = str.format('{},{}\n',i.get('title').replace(',', '，'),pan)
                test.encode('utf-8')
                self.Download('国模拟判断题.csv',test)
            except:
                print()
    def Download(self,name,test):
        f = open(name,'a',encoding='utf-8')
        f.write(test)
if __name__ == '__main__':

    ww = XX()
    num = 1
    while True:
        num += 1
        time.sleep(2)
        ww.Qing()
        print('第'+str(num)+'次爬取')